package com.cms.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.cms.model.TerraceInfo;
import com.service.AbstractDateService;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

/**
 * @author luoyh
 * @date 2014‎-‎3‎-24
 */
public class TerraceInfoServiceImpl extends AbstractDateService implements TerraceInfoService {

	private Logger logger;
	
	public TerraceInfoServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
	}
	@Override
	public TerraceInfo getTerraceInfo(int operatorsId) {
		// TODO Auto-generated method stub
		String sql = "select * from t_terrace where id=" + String.valueOf(operatorsId) + "";
		ModelGenerator<TerraceInfo> dbcpmg = new ModelGenerator<TerraceInfo>(TerraceInfo.class);
		return dbcpmg.generate(base, sql);
	}

	@Override
	public List<TerraceInfo> getTerraceInfos() {
		String sql = "select * from t_terrace where isdel='0' order by id desc";
		logger.debug("sql: " + sql);
		ModelGenerator<TerraceInfo> dbcpmg = new ModelGenerator<TerraceInfo>(TerraceInfo.class);
		return dbcpmg.generateList(base, sql);
	}

	@Override
	public boolean insertTerraceInfo(TerraceInfo terrace) {
		terrace.setId(getTableId("t_terrace"));
		String sql = 
			"insert into t_terrace(id,code,operatorsName,url,loginName,password,contractPeriod,remark) " +
			"values(" + String.valueOf(terrace.getId()) + ",'" + terrace.getCode()+ "','"  +
			terrace.getOperatorsName() + "','"+ terrace.getUrl() + "','" + terrace.getLoginName() + "','" + 
			terrace.getPassword()+ "','"+terrace.getContractPeriod()+"','"+terrace.getRemark()+"')";
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public boolean updateTerraceInfo(TerraceInfo terrace) {
		String sql = 
			"update t_terrace set code='" + terrace.getCode() + "'," +
					"url='" + terrace.getUrl() + "'," +
					"operatorsName='" + terrace.getOperatorsName() + "'," +
					"loginName='" + terrace.getLoginName() + "'," + 
					"password='" + terrace.getPassword() + "'," +
					"contractPeriod='" + terrace.getContractPeriod() + "'," +
					"remark='" + terrace.getRemark()+"' " +
					" where id=" + String.valueOf(terrace.getId()) + "";
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public boolean deleteTerraceInfo(TerraceInfo terrace) {
		String sql = "update t_terrace set isdel='1' where id=" + String.valueOf(terrace.getId()) + "";
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public List<TerraceInfo> getTerraceInfos(String txt_keyword) {
		String sql = "select * from t_terrace where 1=1 and isdel='0' ";
		if(txt_keyword!=null && txt_keyword.length()>0)
		{
			sql+=" and operatorsName like '%"+txt_keyword+"%' ";
		}
		sql+=" order by id desc";
		logger.debug("sql: " + sql);
		ModelGenerator<TerraceInfo> dbcpmg = new ModelGenerator<TerraceInfo>(TerraceInfo.class);
		return dbcpmg.generateList(base, sql);
	}
	@Override
	public List<TerraceInfo> getQuelyTerraceNames() {
		String sql="select id,operatorsName from t_terrace";
		logger.debug("sql: " + sql);
		ModelGenerator<TerraceInfo> dbcpmg = new ModelGenerator<TerraceInfo>(TerraceInfo.class);
		return dbcpmg.generateList(base, sql);
	}
	@Override
	public List<TerraceInfo> getQuelyTerraceNamesByAppNumber(String appnumber) {
		String sql = "select t1.id,t1.operatorsName from t_terrace t1 where t1.id in " +
				"(select appt.terraceId from t_appversionterrace appt where appt.appversionId in " +
					"(select appv.id from t_appversioninfo appv where appv.appnumber='"+appnumber+"'))";
		logger.debug("sql: " + sql);
		ModelGenerator<TerraceInfo> dbcpmg = new ModelGenerator<TerraceInfo>(TerraceInfo.class);
		return dbcpmg.generateList(base, sql);
	}
}
